﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using YodaMaster.Models;
using System.Data.SqlClient;
using System.Data;
using System.ComponentModel.DataAnnotations;


namespace YodaMaster.Controllers
{
    public class ScheduleController : Controller
    {
        //
        // GET: /Schedule/



        ProgDatabase DB = new ProgDatabase();

        ScheduleDB db = new ScheduleDB();

        public void Schedule_Initializer(Dictionary<Course, string> courses)
        {
            foreach (var course in courses.Keys) // Add courses to db
            {
                var item = new ScheduleCourses()
                {
                    faculty = course.faculty,
                    id = course.number,
                    name = course.name
                };
                db.DBcourses.Add(item);

            }
            db.SaveChanges();
            var item9 = new ScheduleParams() // add schedule parametrs to db
            {
                length = courses.Keys.Count(),
                cycle = 1,
                Course_Count = -1,
                number = 1,
                course_counter = courses.Keys.Count() - 1

            };
            db.schedule_params.Add(item9);
            db.SaveChanges();





            foreach (Course course in courses.Keys) // add teaching groups to lists
            {
                db.schedule_params.ToList().First().Course_Count++;
                int count = db.schedule_params.First().Course_Count;


                if (course.lectureHours > 0)
                {
                    foreach (TeachingGroup lecgroup in course.lectureList)
                    {
                        db.Teaching_Groups.Add(new SchedGroupCourse { groupType = YodaMaster.Models.TeachingGroup.TGType.Lecture, groupNumber = lecgroup.groupNumber, id = course.number, name = course.name, faculty = course.faculty, type = "Lecture" });

                    }
                }
                if (course.tutorialHours > 0)
                {
                    foreach (TeachingGroup tutgroup in course.tutorialList)
                    {
                        db.Teaching_Groups.Add(new SchedGroupCourse { groupType = YodaMaster.Models.TeachingGroup.TGType.Tutorial, groupNumber = tutgroup.groupNumber, id = course.number, name = course.name, faculty = course.faculty, type = "Tutorial" });
                    }
                }
                if (course.labHours > 0)
                {
                    foreach (TeachingGroup labgroup in course.labList)
                    {
                        db.Teaching_Groups.Add(new SchedGroupCourse { groupType = YodaMaster.Models.TeachingGroup.TGType.Lab, groupNumber = labgroup.groupNumber, id = course.number, name = course.name, faculty = course.faculty, type = "Lab" });
                    }
                }
                db.SaveChanges();
            }

            db.SaveChanges();


            foreach (Course course in courses.Keys)  // count teaching groups
            {
                db.schedule_params.First().Course_Count++;
                int count = db.schedule_params.First().Course_Count;
                var leclist = (from c in db.Teaching_Groups where c.type == "Lecture" && c.id == course.number select c);
                var tutlist = (from c in db.Teaching_Groups where c.type == "Tutorial" && c.id == course.number select c);
                var lablist = (from c in db.Teaching_Groups where c.type == "Lab" && c.id == course.number select c);

                var items = new ScheduleCounters()
                 {

                     lecgroups_count = leclist.Count(),
                     tutgroups_count = tutlist.Count(),
                     labgroups_count = lablist.Count()
                 };
                db.schedule_counters.Add(items);
                db.SaveChanges();
            }
            db.SaveChanges();


            db.schedule_params.First().Course_Count = -1;    // count num of lists and number of possible schedules

            foreach (Course course in courses.Keys)
            {
                db.schedule_params.First().Course_Count++;
                int count = db.schedule_params.First().Course_Count;
                NumOfLists var = new NumOfLists()
                {
                    num_of_Lists = 0
                };
                if (course.lectureHours > 0)
                {
                    var.num_of_Lists++;
                    db.schedule_params.First().number = db.schedule_counters.ToList().ElementAt(count).lecgroups_count * db.schedule_params.First().number;
                }
                if (course.tutorialHours > 0)
                {
                    var.num_of_Lists++;
                    db.schedule_params.First().number = db.schedule_counters.ToList().ElementAt(count).tutgroups_count * db.schedule_params.First().number;
                }
                if (course.labHours > 0)
                {
                    var.num_of_Lists++;
                    db.schedule_params.First().number = db.schedule_counters.ToList().ElementAt(count).labgroups_count * db.schedule_params.First().number;
                }

                db.nums.Add(var);
                db.SaveChanges();
            }
            db.SaveChanges();

            // find overlap groups

            int flag;



            foreach (var item in db.Teaching_Groups)
                foreach (var item2 in db.Teaching_Groups)
                {
                    if (!item.Equals(item2))
                    {
                        flag = 1;

                        var course1 = (from c in courses.Keys where c.number == item.id select c).Single();
                        var course2 = (from c in courses.Keys where c.number == item2.id select c).Single();

                        List<TeachingGroup> groups1 = new List<TeachingGroup>();
                        List<TeachingGroup> groups2 = new List<TeachingGroup>();

                        if (item.type == "Lecture")
                        {
                            groups1 = course1.lectureList;
                        }

                        if (item.type == "Tutorial")
                        {
                            groups1 = course1.tutorialList;
                        }


                        if (item.type == "Lab")
                        {
                            groups1 = course1.labList;
                        }


                        if (item2.type == "Lecture")
                        {
                            groups2 = course2.lectureList;

                        }
                        if (item2.type == "Tutorial")
                        {
                            groups2 = course2.tutorialList;

                        }
                        if (item2.type == "Lab")
                        {
                            groups2 = course2.labList;
                        }

                        var group1 = (from c in groups1 where c.groupNumber == item.groupNumber select c).Single();
                        var group2 = (from c in groups2 where c.groupNumber == item2.groupNumber select c).Single();

                        foreach (TeachingHour time1 in group1.teachingHours)
                            foreach (TeachingHour time2 in group2.teachingHours)
                            {
                                int result = compare_dates.compare(time1.startTime, time1.endTime, time2.startTime, time2.endTime);
                                if (result == 0 && flag == 1) // two groups overlap
                                {

                                    var list = db.overlap_groups.ToList();
                                    bool flag2 = false;
                                    foreach (var item3 in list)
                                    {
                                        if (item3.groupNumber1 == item.groupNumber && item3.id1 == item.id)
                                            if (item3.groupNumber2 == item2.groupNumber && item3.id2 == item2.id)
                                                flag2 = true;
                                        if (item3.groupNumber2 == item.groupNumber && item3.id2 == item.id)
                                            if (item3.groupNumber1 == item2.groupNumber && item3.id1 == item2.id)
                                                flag2 = true;
                                    }

                                    if (flag2 == false)
                                    {
                                        db.overlap_groups.Add(new ScheduleGroups
                                        {
                                            faculty1 = item.faculty,
                                            faculty2 = item2.faculty,
                                            groupNumber1 = item.groupNumber,
                                            groupNumber2 = item2.groupNumber,
                                            groupType1 = item.groupType,
                                            groupType2 = item2.groupType,
                                            id1 = item.id,
                                            id2 = item2.id,
                                            name1 = item.name,
                                            name2 = item.name
                                        });
                                        db.SaveChanges();
                                    }
                                    flag = 0;
                                }

                            }
                    }
                }

            db.SaveChanges();


        }






        public ActionResult Index()
        {


            Database.DefaultConnectionFactory = new System.Data.Entity.Infrastructure.SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");



            MyCart cart = MyCart.GetCart(this.HttpContext);
            Dictionary Dict = new Dictionary();
            Dict.Courses = new Dictionary<Course, string>();
            List<ScheduleCart> List = cart.GetCartItems();

            if (List.Count == 0)
            {
                return RedirectToAction("EmptyAlert");
            }

            // convert list<Cart> to Dictionary of courses
            string CartId = cart.GetCartId(this.HttpContext);
            foreach (var course in DB.Courses.Keys)
            {
                foreach (var cartItem in List)
                {
                    if (cartItem.id == course.number)
                    {
                        Dict.Courses.Add(course, course.faculty);
                        break;
                    }
                }

            }
            foreach (var item in db.DBcourses)
            {
                db.DBcourses.Remove(item);
            }
            foreach (var item in db.nums)
            {
                db.nums.Remove(item);
            }
            foreach (var item in db.overlap_groups)
            {
                db.overlap_groups.Remove(item);
            }
            foreach (var item in db.schedule)
            {
                db.schedule.Remove(item);
            }
            foreach (var item in db.schedule_counters)
            {
                db.schedule_counters.Remove(item);
            }
            foreach (var item in db.schedule_params)
            {
                db.schedule_params.Remove(item);
            }
            foreach (var item in db.Teaching_Groups)
            {
                db.Teaching_Groups.Remove(item);
            }
            db.SaveChanges();
            Schedule_Initializer(Dict.Courses);

            db.SaveChanges();
            return RedirectToAction("First_Schedule");
        }

        public ActionResult Logout()
        {
            
            string[] myCookies = Request.Cookies.AllKeys;
            foreach (string cookie in myCookies)
            {
                Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
            }
            Session.Clear();
            foreach (var item in db.myschedule)
            {
                db.myschedule.Remove(item);
                db.SaveChanges();
            }

             
            return RedirectToAction("Logout", "Login");
        }

        public ActionResult First_Schedule() // create first schedule
        {
            foreach (var item in db.DBcourses)
            {
                ScheduleModel schedulemodel = new ScheduleModel();
                schedulemodel.numofschedule = 0;
                schedulemodel.course_counter = db.schedule_params.First().length - 1;
                schedulemodel.cycle = 1;
                schedulemodel.index_lec = 0;
                schedulemodel.index_lab = 0;
                schedulemodel.index_tut = 0;
                db.schedule.Add(schedulemodel);
                db.SaveChanges();
            }
            return RedirectToAction("Schedule_Courses_forward");
        }

        public ActionResult Schedule_Courses_backward()
        {
            if (db.schedule.First().numofschedule == 1)
            {
               return RedirectToAction("../ScheduleCart/Index");
            }
            int length;
            Schedule schedule = new Schedule(db.schedule.First().numofschedule);
            length = db.DBcourses.Count();
            schedule.index_lec = new List<int>();
            schedule.index_tut = new List<int>();
            schedule.index_lab = new List<int>();


            schedule.SchedCourses = new Dictionary<Course, string>();
            foreach (var item in db.DBcourses)
            {
                var item2 = (from c in DB.Courses.Keys where c.number == item.id select c).Single();
                schedule.SchedCourses.Add(item2, item2.faculty);

            }

            if (db.schedule.First().numofschedule == 0)
            {
                for (int i = 0; i < length; i++)
                {
                    schedule.index_lec.Add(0);
                    schedule.index_tut.Add(0);
                    schedule.index_lab.Add(0);
                }
            }


            db.schedule.First().cycle = 1;
            db.schedule.First().numofschedule--;
            schedule.numofschedule = db.schedule.First().numofschedule;
            schedule.schedule.Clear();
            Schedule_Controller_funcs_back funcs = new Schedule_Controller_funcs_back();
            var list = db.schedule.ToList();
            var list1 = new List<int>();
            var list2 = new List<int>();
            var list3 = new List<int>();
            foreach (var item in list)
            {
                list1.Add(item.index_lec);
                list2.Add(item.index_lab);
                list3.Add(item.index_tut);
            }
            schedule.index_lec = list1;
            schedule.index_lab = list2;
            schedule.index_tut = list3;
            schedule.Counters = db.schedule_counters.ToList();
            schedule.parameters = db.schedule_params.First();
            schedule.groups = db.Teaching_Groups.ToList(); // all teaching groups
            schedule.overlap_groups = db.overlap_groups.ToList();
            schedule.course_counter = db.schedule_params.First().length - 1;
            schedule.num_of_lists = new List<int>();
            var item5 = db.nums;
            foreach (var item6 in item5)
                schedule.num_of_lists.Add(item6.num_of_Lists);

            Schedule current = schedule;
            current.cycle = 1;
            if (current.numofschedule > 0)
            {
                db.schedule_params.First().Course_Count = -1;
                int p;
                if (current.numofschedule > 0)
                {
                    p = db.schedule.First().course_counter;
                    while (current.cycle == 1) // choose course
                    {
                        p = current.course_counter;
                        if (schedule.num_of_lists[p] == 3) funcs.choose_from_three(ref current);
                        else if (schedule.num_of_lists[p] == 2 && db.schedule_counters.ToList().ElementAt(p).labgroups_count == 0) funcs.choose_from_two_no_lab(ref  current);
                        else if (schedule.num_of_lists[p] == 2 && db.schedule_counters.ToList().ElementAt(p).tutgroups_count == 0) funcs.choose_from_two_no_tut(ref current);
                        else if (schedule.num_of_lists[p] == 1 && db.schedule_counters.ToList().ElementAt(p).labgroups_count > 0) funcs.choose_from_labs(ref current);
                        else if (schedule.num_of_lists[p] == 1 && db.schedule_counters.ToList().ElementAt(p).tutgroups_count > 0) funcs.choose_from_tutorials(ref current);
                        else if (schedule.num_of_lists[p] == 1 && db.schedule_counters.ToList().ElementAt(p).lecgroups_count > 0) funcs.choose_from_lectures(ref  current);

                    }


                }
                funcs.AddCoursesToSchedule(ref current);
                funcs.CheckOverlapGroups(ref current);
                foreach (var item in db.schedule)
                    db.schedule.Remove(item);
                for (int i = 0; i < length; i++)
                {
                    var model = new ScheduleModel()
                    {
                        numofschedule = current.numofschedule,
                        cycle = current.cycle,
                        course_counter = current.course_counter,
                        Type = current.Type,
                        index_lab = current.index_lab.ElementAt(i),
                        index_lec = current.index_lec.ElementAt(i),
                        index_tut = current.index_tut.ElementAt(i)
                    };
                    db.schedule.Add(model);
                }
                db.SaveChanges();
            }
         
            current.table = new TimeTable();
            foreach (var item in current.schedule)
                current.table.AddTeachingGroup(item);
            return View(current);


        }


        public ActionResult Schedule_Courses_forward()
        {
           
            if (db.schedule.First().numofschedule == db.schedule_params.First().number)
            {
                return RedirectToAction("../ScheduleCart/Index");
            }
            int length;
            Schedule schedule = new Schedule(db.schedule.First().numofschedule);
            length = db.DBcourses.Count();
            schedule.index_lec = new List<int>();
            schedule.index_tut = new List<int>();
            schedule.index_lab = new List<int>();


            schedule.SchedCourses = new Dictionary<Course, string>();
            foreach (var item in db.DBcourses)
            {
                var item2 = (from c in DB.Courses.Keys where c.number == item.id select c).Single();
                schedule.SchedCourses.Add(item2, item2.faculty);

            }

            if (db.schedule.First().numofschedule == 0)
            {
                for (int i = 0; i < length; i++)
                {
                    schedule.index_lec.Add(0);
                    schedule.index_tut.Add(0);
                    schedule.index_lab.Add(0);
                }
            }


            db.schedule.First().cycle = 1;
            db.schedule.First().numofschedule++;
            schedule.numofschedule = db.schedule.First().numofschedule;
            schedule.schedule.Clear();
            Schedule_Controller_funcs funcs = new Schedule_Controller_funcs();
            var list = db.schedule.ToList();
            var list1 = new List<int>();
            var list2 = new List<int>();
            var list3 = new List<int>();
            foreach (var item in list)
            {
                list1.Add(item.index_lec);
                list2.Add(item.index_lab);
                list3.Add(item.index_tut);
            }
            schedule.index_lec = list1;
            schedule.index_lab = list2;
            schedule.index_tut = list3;
            schedule.Counters = db.schedule_counters.ToList();
            schedule.parameters = db.schedule_params.First();
            schedule.groups = db.Teaching_Groups.ToList(); // all teaching groups
            schedule.overlap_groups = db.overlap_groups.ToList();
            schedule.course_counter = db.schedule_params.First().length - 1;
            schedule.num_of_lists = new List<int>();
            var item5 = db.nums;
            foreach (var item6 in item5)
                schedule.num_of_lists.Add(item6.num_of_Lists);

            Schedule current = schedule;
            current.cycle = 1;
            if (current.numofschedule <= db.schedule_params.First().number)
            {
                db.schedule_params.First().Course_Count = -1;
                int p;
                if (current.numofschedule > 1)
                {
                    p = db.schedule.First().course_counter;
                    while (current.cycle == 1) // choose course
                    {
                        p = current.course_counter;
                        if (schedule.num_of_lists[p] == 3) funcs.choose_from_three(ref current);
                        else if (schedule.num_of_lists[p] == 2 && db.schedule_counters.ToList().ElementAt(p).labgroups_count == 0) funcs.choose_from_two_no_lab(ref  current);
                        else if (schedule.num_of_lists[p] == 2 && db.schedule_counters.ToList().ElementAt(p).tutgroups_count == 0) funcs.choose_from_two_no_tut(ref current);
                        else if (schedule.num_of_lists[p] == 1 && db.schedule_counters.ToList().ElementAt(p).labgroups_count > 0) funcs.choose_from_labs(ref current);
                        else if (schedule.num_of_lists[p] == 1 && db.schedule_counters.ToList().ElementAt(p).tutgroups_count > 0) funcs.choose_from_tutorials(ref current);
                        else if (schedule.num_of_lists[p] == 1 && db.schedule_counters.ToList().ElementAt(p).lecgroups_count > 0) funcs.choose_from_lectures(ref  current);

                    }


                }
                funcs.AddCoursesToSchedule(ref current);
                funcs.CheckOverlapGroups(ref current);
                foreach (var item in db.schedule)
                    db.schedule.Remove(item);
                for (int i = 0; i < length; i++)
                {
                    var model = new ScheduleModel()
                    {
                        numofschedule = current.numofschedule,
                        cycle = current.cycle,
                        course_counter = current.course_counter,
                        Type = current.Type,
                        index_lab = current.index_lab.ElementAt(i),
                        index_lec = current.index_lec.ElementAt(i),
                        index_tut = current.index_tut.ElementAt(i)
                    };
                    db.schedule.Add(model);
                }
                db.SaveChanges();
            }
         
            current.table = new TimeTable();
            foreach (var item in current.schedule)
                current.table.AddTeachingGroup(item);

            return View(current);


        }
        public ActionResult View_Tests()
        {
            MyCart cart = MyCart.GetCart(this.HttpContext);
            Dictionary Dict = new Dictionary();
            Dict.Courses = new Dictionary<Course, string>();
            List<ScheduleCart> List = cart.GetCartItems();

            if (List.Count == 0)
            {
                return RedirectToAction("EmptyAlert");
            }

            // convert list<Cart> to Dictionary of courses
            string CartId = cart.GetCartId(this.HttpContext);
            foreach (var course in DB.Courses.Keys)
            {
                foreach (var cartItem in List)
                {
                    if (cartItem.id == course.number)
                    {
                        Dict.Courses.Add(course, course.faculty);
                        break;
                    }
                }

            }

            return View(Dict.Courses.Keys);
        }
        public ActionResult SaveSchedule()
        {
            foreach (var item in db.myschedule)
                     db.myschedule.Remove(item);
            foreach (var item in db.myDBcourses)
                     db.myDBcourses.Remove(item);
            foreach (var item in db.myoverlap_groups)
                    db.myoverlap_groups.Remove(item);
            foreach (var item in db.myschedule_counters)
                db.myschedule_counters.Remove(item);
            foreach (var item in db.myschedule_params)
                db.myschedule_params.Remove(item);
            foreach (var item in db.myTeaching_Groups)
                   db.myTeaching_Groups.Remove(item);
            
            foreach (var item in db.schedule)
            {
                var item2 = new MyScheduleModel()
                {
                    course_counter = item.course_counter,
                    Type = item.Type,
                    numofschedule = item.numofschedule,
                    cycle = item.cycle,
                    index_lab = item.index_lab,
                    index_lec = item.index_lec,
                    index_tut = item.index_tut
                };
                db.myschedule.Add(item2);
                db.SaveChanges();
            }
            foreach (var item in db.DBcourses)
            {
                var item2 = new MyScheduleCourses()
                {
                    faculty = item.faculty,
                    course = item.course,
                    id = item.id,
                    name = item.name

                };
                db.myDBcourses.Add(item2);
                db.SaveChanges();
            }
            foreach (var item in db.overlap_groups)
            {
                var item2 = new MyScheduleGroups()
                {
                   type1 = item.type1,
                   type2 = item.type2,
                   faculty1 = item.faculty1,
                   faculty2 = item.faculty2,
                   groupNumber1 = item.groupNumber1,
                   groupNumber2 = item.groupNumber2,
                   groupType1 = item.groupType1,
                   groupType2 = item.groupType2,
                   id1 = item.id1,
                   id2 = item.id2,
                   name1 = item.name1,
                   name2 = item.name2


                };
                db.myoverlap_groups.Add(item2);
                db.SaveChanges();
            }
            foreach (var item in db.schedule_counters)
            {
                var item2 = new MyScheduleCounters()
                {
                   labgroups_count = item.labgroups_count,
                   lecgroups_count = item.lecgroups_count,
                   tutgroups_count = item.tutgroups_count
                };
                db.myschedule_counters.Add(item2);
                db.SaveChanges();
            }
            foreach (var item in db.schedule_params)
            {
                var item2 = new MyScheduleParams()
                {
                    course_counter = item.course_counter,
                    Course_Count = item.Course_Count,
                    cycle = item.cycle,
                    length = item.length,
                    number = item.number
                };
                db.myschedule_params.Add(item2);
                db.SaveChanges();
            }
            foreach (var item in db.Teaching_Groups)
            {
                var item2 = new MySchedGroupCourse()
                {
                    faculty = item.faculty,
                    type = item.type,
                    groupNumber = item.groupNumber,
                    name = item.name,
                    id = item.id,
                    groupType = item.groupType
                };
                db.myTeaching_Groups.Add(item2);
                db.SaveChanges();
            }

            return RedirectToAction("MySchedule");
        }

        public ActionResult RemoveMySchedule()
        {
            foreach (var item in db.myschedule)
            {
                db.myschedule.Remove(item);
                db.SaveChanges();
            }
            return View();
        }
        public ActionResult MySchedule()
        {
            if (db.myschedule.Count() == 0)
            {
                return RedirectToAction("NoSavedSchedule");
            }
            int length;
            Schedule schedule = new Schedule(db.myschedule.First().numofschedule);
            length = db.myDBcourses.Count();
            schedule.index_lec = new List<int>();
            schedule.index_tut = new List<int>();
            schedule.index_lab = new List<int>();


            schedule.SchedCourses = new Dictionary<Course, string>();
            foreach (var item in db.myDBcourses)
            {
                var item2 = (from c in DB.Courses.Keys where c.number == item.id select c).Single();
                schedule.SchedCourses.Add(item2, item2.faculty);

            }

            if (db.myschedule.First().numofschedule == 0)
            {
                for (int i = 0; i < length; i++)
                {
                    schedule.index_lec.Add(0);
                    schedule.index_tut.Add(0);
                    schedule.index_lab.Add(0);
                }
            }


            db.myschedule.First().cycle = 1;
            schedule.numofschedule = db.myschedule.First().numofschedule;
            schedule.schedule.Clear();
            Schedule_Controller_funcs funcs = new Schedule_Controller_funcs();

            var list = db.myschedule.ToList();
            var list1 = new List<int>();
            var list2 = new List<int>();
            var list3 = new List<int>();
            foreach (var item in list)
            {
                list1.Add(item.index_lec);
                list2.Add(item.index_lab);
                list3.Add(item.index_tut);
            }
            schedule.index_lec = list1;
            schedule.index_lab = list2;
            schedule.index_tut = list3;
            schedule.Counters = new List<ScheduleCounters>();
            schedule.parameters = new ScheduleParams();
            schedule.groups = new List<SchedGroupCourse>();
            schedule.overlap_groups = new List<ScheduleGroups>();


            foreach (var item in db.myschedule_counters)
            {
                var item2 = new ScheduleCounters()
                {
                    labgroups_count = item.labgroups_count,
                    lecgroups_count = item.lecgroups_count,
                    tutgroups_count = item.tutgroups_count
                };

                schedule.Counters.Add(item2);
            }
            
                var  param = new ScheduleParams
                {
                    Course_Count = db.myschedule_params.First().Course_Count,
                    course_counter = db.myschedule_params.First().course_counter,
                    cycle = db.myschedule_params.First().cycle,
                    length = db.myschedule_params.First().length,
                    number = db.myschedule_params.First().number
                };
                schedule.parameters = param;

             foreach (var item in db.myTeaching_Groups)
                {
                    var item2 = new SchedGroupCourse()
                    {
                        faculty = item.faculty,
                        groupNumber = item.groupNumber,
                        type = item.type,
                        name = item.name,
                        id = item.id,
                        groupType = item.groupType
                    };

                    schedule.groups.Add(item2);
                }
             foreach (var item in db.myoverlap_groups)
             {
                 var item2 = new ScheduleGroups()
                 {
                    faculty1 = item.faculty1,
                    faculty2 = item.faculty2,
                    groupNumber1 = item.groupNumber1,
                    groupNumber2 = item.groupNumber2,
                    groupType1 = item.groupType2,
                    groupType2 = item.groupType2,
                    id1 = item.id1,
                    id2 = item.id2,
                    name1 = item.name1,
                    name2 = item.name2,
                    type1 = item.type1,
                    type2 = item.type2
                    
                 };

                 schedule.overlap_groups.Add(item2);
             }
            
           
            schedule.course_counter = db.myschedule_params.First().length - 1;

            schedule.cycle = 1;

            funcs.AddCoursesToSchedule(ref schedule);
                funcs.CheckOverlapGroups(ref schedule);
            
              schedule.table = new TimeTable();
              foreach (var item in schedule.schedule)
                schedule.table.AddTeachingGroup(item);

            return View(schedule.table);
        }

        public ActionResult EmptyAlert()
        {
            TempData["alertMessage"] = "It Is Not Possible To Build Schedules! Your Cart Is Empty!";
            return View();
        }
        public ActionResult NoSavedSchedule()
        {
            TempData["alertMessage"] = "You Must Save A Schedule First!";
            return View();
        }
        public ActionResult NoSchedule()
        {
            TempData["alertMessage"] = "No Schedules Are Available!";
            return View();
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}
    



